<?php
define('NO_CHECK_LOGIN','turn');
//载入数函数库
require'init.php';
if($_POST){
	$captcha=I('captcha','post','string');
	$usernumber = I('usernumber','post','html');
	$userpassword=I('userpassword','post','html');
	//查询数据库---------------------------------------------------------------
	$result=mysqli_query($link,"SELECT * FROM `usertable` WHERE `usernumber`='$usernumber'");
	//获取全部记录
	$data = mysqli_fetch_assoc($result);
    //-----------------------------------------------------------------------
	if(!checkCode($captcha)){
		echo "<script>alert ('登陆失败：验证码错误');</script>";
		redirect('../view/login.html');
		display([false,'登陆失败：验证码错误']);
	}
	if($data && $userpassword==$data['userpassword']){
		//保存信息到Seesion
		$_SESSION['cms']['admin']=['userid'=>$data['userid'],'username'=>$data['username']];
		//跳转到首页
		redirect('../model/index.php');
		}else{
		echo "<script>alert ('登陆失败：用户名或密码错误');</script>";
		redirect('../view/login.html');
		display([false,'登陆失败：验证码错误']);
    }
}
$action=I('a','get','string');
if($action=='logout'){
	unset($_SESSION['cms']['admin']);
}
//------------------------------------------
function display($msg=null){
	require '../view/login.html';
	exit;
}
function checkCode($code){
	$captcha = $_SESSION['cms']['captcha'];
	if(!empty($captcha)){
		unset($_SESSION['cms']['$captcha']);
		return strtoupper($captcha) == strtoupper($code);
	}
	return false;
}
require '../view/login.html';
?>